package net.hn.hnms.biz.wms.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.biz.wms.convert.SupplierConvert;
import net.hn.hnms.biz.wms.domain.Supplier;
import net.hn.hnms.biz.wms.pojo.query.SupplierQuery;
import net.hn.hnms.biz.wms.pojo.vo.ShipmentOrderDetailVO;
import net.hn.hnms.biz.wms.pojo.vo.SupplierVO;
import net.hn.hnms.biz.wms.service.SupplierService;
import net.hn.hnms.sys.common.core.web.controller.BaseController;
import net.hn.hnms.sys.common.excel.utils.ExcelUtil;
import net.hn.hnms.sys.common.log.annotation.Log;
import net.hn.hnms.sys.common.log.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 供应商Controller
 *
 * @author zcc
 * @date 2022-08-05
 */
@Api(tags = "供应商接口列表")
@RestController
@RequestMapping("/wms/supplier")
public class SupplierController extends BaseController {
    @Autowired
    private SupplierService service;
    @Autowired
    private SupplierConvert convert;

    @ApiOperation("查询供应商列表")
    @PostMapping("/list")
    public ResponseEntity<Page<Supplier>> list(@RequestBody SupplierQuery query, Pageable page) {
        List<Supplier> list = service.selectList(query, page);
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page) list).getTotal()));
    }

    @ApiOperation("导出供应商列表")
    @Log(title = "供应商", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, SupplierQuery query) {
        List<Supplier> list = service.selectList(query, null);
        ExcelUtil.exportExcel(convert.dos2vos(list), "供应商数据", SupplierVO.class, response);
    }

    @ApiOperation("获取供应商详细信息")
    @GetMapping(value = "/{id}")
    public ResponseEntity<Supplier> getInfo(@PathVariable("id") Long id) {
        return ResponseEntity.ok(service.selectById(id));
    }

    @ApiOperation("新增供应商")
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody Supplier supplier) {
        return ResponseEntity.ok(service.insert(supplier));
    }

    @ApiOperation("修改供应商")
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody Supplier supplier) {
        return ResponseEntity.ok(service.update(supplier));
    }

    @ApiOperation("删除供应商")
    @Log(title = "供应商", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResponseEntity<Integer> remove(@PathVariable Long[] ids) {
        return ResponseEntity.ok(service.deleteByIds(ids));
    }
}
